from sklearn.datasets import load_digits
from sklearn.neural_network import MLPClassifier # 导入神经网络模型
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import confusion_matrix,classification_report
from sklearn.model_selection import train_test_split
from sklearn.metrics import mean_squared_error, r2_score

digits = load_digits()
X = digits.data
Y = digits.target

scaler = StandardScaler()
X_scaled = scaler.fit_transform(X)

X_train, X_test, y_train, y_test = train_test_split(
    X_scaled, Y, test_size=0.3, random_state=1
) # 划分训练集和测试集

model = MLPClassifier()

model.fit(X_train, y_train)
acc = model.score(X_test, y_test)
print(f"Accuracy: {acc}")
y_pred = model.predict(X_test)
print(y_pred[:10],y_test[:10])

print(f"{classification_report(y_true=y_test, y_pred=y_pred)}")
print(f"{confusion_matrix(y_true=y_test, y_pred=y_pred)}")

mse = mean_squared_error(y_test, y_pred)
r_2 = r2_score(y_test, y_pred)
print('\n',f"MSE: {mse}")
print(f"R2: {r_2}")